package org.unt.genlogic;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Dates {

	public static void main(String[] args) throws IOException {

		System.out.println(
		dateToString(new java.util.Date(), "yyyyMMdd"));
		
		// Calendar cal = new GregorianCalendar(2011, 9, 10);
		// System.out.println(getWeekNumberFromDate(cal.getTime()));

		// Calendar cal = new GregorianCalendar(2011, 10, 21);
		// System.out.println(getWeekNumberFromDate(cal.getTime()));

		// Calendar c = Calendar.getInstance().
		// Date d = new Date(1228107600000L);
		// Date d1 = new Date("2008-12-01");
		// System.out.println(d);
		// System.out.println(Dates.dateToString(d, "yyyy-MM-dd"));
	}

	public static void a() {

		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int week = cal.get(Calendar.WEEK_OF_YEAR);
		
		System.out.println("curr week " + week);
		
		System.out.println("from " + (year - 1) + " week " + week);
		System.out.println("to   " + year + " week " + week);
		
		cal.set(Calendar.WEEK_OF_YEAR, 0);
		System.out.println(cal.getTime());
		System.out.println(cal.get(Calendar.WEEK_OF_YEAR));
	}

	public static java.util.Date fromSqlToUtilDate(java.sql.Date sqlDate) {

		long t = sqlDate.getTime();
		return new java.util.Date(t);
	}

	public static java.sql.Date fromUtilToSqlDate(java.util.Date utilDate) {

		long t = utilDate.getTime();
		return new java.sql.Date(t);
	}

	public static String dateToString(long l) {
		return dateToString(new java.util.Date(l));
	}

	public static String dateToString(java.util.Date utilDate) {

		if (utilDate == null) {
			return null;
		}

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return formatter.format(utilDate);
	}

	public static String dateToString(java.util.Date utilDate, String format) {

		if (utilDate == null) {
			return null;
		}

		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(utilDate);
	}

	public static String dateToString(java.sql.Date sqlDate, String format) {

		if (sqlDate == null) {
			return null;
		}

		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(sqlDate);
	}

	// The week goes from Monday to Sunday
	public static int getWeekNumberFromDate(java.util.Date utilDate) {

		Calendar cal = Calendar.getInstance();
		cal.setTime(utilDate);

		return cal.get(Calendar.WEEK_OF_YEAR);
	}
}
